﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ANZ_AIM_GUI.Utils;
using ANZ_AIM_BLL;
using ANZ_AIM_BLL.Model;
using System.Reflection;


namespace ANZ_AIM_GUI
{
    public partial class frmSalePIPMgmt : Form
    {
        protected AppParam appParam = AppParam.Instance;
        public frmSalePIPMgmt()
        {
            InitializeComponent();

            dtFromDate.Value = DateTime.Now;
            dtToDate.Value = DateTime.Now;
            appParam.Role = User.ROLE_APPROVER;

            if (appParam.Role.Contains(User.ROLE_VERIFIER))
            {
                this.Text = "SALE CHANGE VERIFY";
                groupBox1.Text = "Need To Verify";
                groupBox3.Visible = false;
                groupBox4.Visible = false;
            }
            else if (appParam.Role.Contains(User.ROLE_APPROVER))
            {
                this.Text = "SALE CHANGE APPROVE";
                groupBox3.Visible = true;
                groupBox4.Visible = true;
            }

            //initdata
            InitData();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string fromDate = dtFromDate.Value.ToShortDateString();
            string toDate = dtToDate.Value.AddDays(1).ToShortDateString();

            string status = "";
            if (appParam.Role.Contains(User.ROLE_VERIFIER))
            {
                status = Constant.NEED_TO_VERIFY;
            }
            else if (appParam.Role.Contains(User.ROLE_APPROVER))
            {
                status = Constant.NEED_TO_APPROVE;
            }

            string batchFile = txtBatchFile.Text.Trim();

            BatchFileSaleInfoUploadBLL batchFileSaleInfoUploadBll = new BatchFileSaleInfoUploadBLL();
            DataTable dt = batchFileSaleInfoUploadBll.Search(status, batchFile, fromDate, toDate);

            LoadData(dt);
        }

        private void InitData()
        {
            string status = "";
            if (appParam.Role.Contains(User.ROLE_VERIFIER))
            {
                status = Constant.NEED_TO_VERIFY;
            }
            else if (appParam.Role.Contains(User.ROLE_APPROVER))
            {
                status = Constant.NEED_TO_APPROVE;
            }

            BatchFileSaleInfoUploadBLL batchFileSaleInfoUploadBll = new BatchFileSaleInfoUploadBLL();
            DataTable dtResult = batchFileSaleInfoUploadBll.GetBatchFileByStatus(status);
            LoadData(dtResult);
            loadDataApproved();
            loadDataPending();
        }

        private void LoadData(DataTable dtData)
        {

            dgvData.DataSource = null;
            dgvData.Columns.Clear();
            dgvData.DataSource = dtData;
            dgvData.AutoResizeColumns();

            dgvData.Columns[0].HeaderText = "Batch File";
            dgvData.Columns[1].HeaderText = "File Name";
            dgvData.Columns[2].HeaderText = "Number";
            dgvData.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dgvData.Columns[3].HeaderText = "Uploaded Date";
            dgvData.Columns[4].Visible = false;
            dgvData.Columns[5].Visible = false;

            //add column action
            DataGridViewLinkColumn colAction = new DataGridViewLinkColumn();
            colAction.HeaderText = "Status";
            colAction.Name = "Status";

            if (appParam.Role.Contains(User.ROLE_VERIFIER))
            {
                colAction.Text = "Verify";
            }
            else if (appParam.Role.Contains(User.ROLE_APPROVER))
            {
                colAction.Text = "Approve";
            }

            colAction.Width = 70;
            colAction.UseColumnTextForLinkValue = true;
            dgvData.Columns.Insert(5, colAction);
        }

        private void dgvData_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                int selectedRowCount = dgvData.Rows.GetRowCount(DataGridViewElementStates.Selected);
                if (selectedRowCount > 0)
                {
                    DataRow dr = ((DataRowView)dgvData.SelectedRows[0].DataBoundItem).Row;
                    frmDataSaleInfoUpload frmDateSaleInfoUpload = new frmDataSaleInfoUpload(dr[0].ToString());
                    frmDateSaleInfoUpload.ShowDialog(this);
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void loadDataApproved()
        {
            BatchFileSaleInfoUploadBLL batchFileSaleInfoUploadBll = new BatchFileSaleInfoUploadBLL();
            DataTable dtResult = batchFileSaleInfoUploadBll.GetBatchFileByStatus(Constant.APPROVED);

            dgvDataApproved.DataSource = null;
            dgvDataApproved.Columns.Clear();
            dgvDataApproved.DataSource = dtResult;

            dgvDataApproved.Columns[0].HeaderText = "Batch File";
            dgvDataApproved.Columns[1].HeaderText = "File Name";
            dgvDataApproved.Columns[2].HeaderText = "Number";
            dgvDataApproved.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dgvDataApproved.Columns[3].HeaderText = "Uploaded Date";
            dgvDataApproved.Columns[4].Visible = false;
            dgvDataApproved.Columns[5].Visible = false;

        }

        private void loadDataPending()
        {
            BatchFileSaleInfoUploadBLL batchFileSaleInfoUploadBll = new BatchFileSaleInfoUploadBLL();
            DataTable dtResult = batchFileSaleInfoUploadBll.GetBatchFileByStatus(Constant.PENDING);

            dgvDataPending.DataSource = null;
            dgvDataPending.Columns.Clear();
            dgvDataPending.DataSource = dtResult;

            dgvDataPending.Columns[0].HeaderText = "Batch File";
            dgvDataPending.Columns[1].HeaderText = "File Name";
            dgvDataPending.Columns[2].HeaderText = "Number";
            dgvDataPending.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            dgvDataPending.Columns[3].HeaderText = "Uploaded Date";
            dgvDataPending.Columns[4].Visible = false;
            dgvDataPending.Columns[5].Visible = false;

        }

        private void dgvDataApproved_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                int selectedRowCount = dgvDataApproved.Rows.GetRowCount(DataGridViewElementStates.Selected);
                if (selectedRowCount > 0)
                {
                    DataRow dr = ((DataRowView)dgvDataApproved.SelectedRows[0].DataBoundItem).Row;
                    frmDataSaleInfoUpload frmDateSaleInfoUpload = new frmDataSaleInfoUpload(dr[0].ToString());
                    frmDateSaleInfoUpload.ShowDialog(this);
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void dgvDataPending_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                int selectedRowCount = dgvDataPending.Rows.GetRowCount(DataGridViewElementStates.Selected);
                if (selectedRowCount > 0)
                {
                    DataRow dr = ((DataRowView)dgvDataPending.SelectedRows[0].DataBoundItem).Row;
                    frmDataSaleInfoUpload frmDateSaleInfoUpload = new frmDataSaleInfoUpload(dr[0].ToString());
                    frmDateSaleInfoUpload.ShowDialog(this);
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void dgvData_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView dgv = sender as DataGridView;
            if (dgv.Columns[e.ColumnIndex].Name == "Status")
            {
                string batchFileSaleInfoID = dgvData.Rows[e.RowIndex].Cells["BatchFileSaleInfoID"].Value.ToString();
                string type = dgvData.Rows[e.RowIndex].Cells["Type"].Value.ToString();
                if (appParam.Role.Contains(User.ROLE_VERIFIER))
                {
                    frmSaleInfoVerifyStatus frm = new frmSaleInfoVerifyStatus(batchFileSaleInfoID, type);
                    DialogResult result = frm.ShowDialog(this);
                    if (result == DialogResult.OK)
                    {
                        InitData();
                    }
                }
                else if (appParam.Role.Contains(User.ROLE_APPROVER))
                {
                    frmSaleInfoApproveStatus frm = new frmSaleInfoApproveStatus(batchFileSaleInfoID, type);
                    DialogResult result = frm.ShowDialog(this);
                    if (result == DialogResult.OK)
                    {
                        InitData();
                    }
                }
            }
        }
    }
}
